package com.tsin.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsin.usercenter.common.BaseResponse;
import com.tsin.usercenter.common.ErrorCode;
import com.tsin.usercenter.common.ResultUtils;
import com.tsin.usercenter.exception.BusinessException;
import com.tsin.usercenter.model.Team;
import com.tsin.usercenter.model.User;
import com.tsin.usercenter.model.dto.TeamQuery;
import com.tsin.usercenter.model.dto.TeamUpdateQuery;
import com.tsin.usercenter.model.request.DeleteTeamRequest;
import com.tsin.usercenter.model.request.JoinTeamRequest;
import com.tsin.usercenter.model.request.QuitTeamRequest;
import com.tsin.usercenter.model.request.TeamRequest;
import com.tsin.usercenter.model.vo.TeamVO;
import com.tsin.usercenter.service.TeamService;
import com.tsin.usercenter.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.tsin.usercenter.constant.UserConstant.USER_LOGIN_KEY;

@RequestMapping("/team")
@RestController
@CrossOrigin(origins = {"http://101.200.127.11"}, allowCredentials = "true")
@Slf4j
@Tag(name = "TeamController")
public class TeamController {
    @Resource
    private UserService userService;
    @Resource
    private TeamService teamService;

    @PostMapping("/add")
    public BaseResponse<Long> addTeam(@RequestBody TeamRequest teamRequest, HttpServletRequest httpServletRequest) {
        if (teamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        long teamId = teamService.addTeam(teamRequest, loginUser);
        return ResultUtils.success(teamId);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(@RequestBody DeleteTeamRequest deleteTeamRequest, HttpServletRequest httpServletRequest) {
        if (deleteTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = deleteTeamRequest.getId();
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        Boolean result = teamService.deleteTeam(id, loginUser);
        if (!result) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateQuery teamUpdateQuery, HttpServletRequest httpServletRequest) {
        if (teamUpdateQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        Boolean updateResult = teamService.updateTeam(teamUpdateQuery, loginUser);
        if (!updateResult) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(true);

    }

    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody JoinTeamRequest joinTeamRequest, HttpServletRequest httpServletRequest) {
        if (joinTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        Boolean updateResult = teamService.joinTeams(joinTeamRequest, loginUser);
        if (!updateResult) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(true);

    }

    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(@RequestBody QuitTeamRequest quitTeamRequest, HttpServletRequest httpServletRequest) {
        if (quitTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        Boolean quitTeams = teamService.quitTeams(quitTeamRequest, loginUser);
        if (!quitTeams) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(true);

    }

    @GetMapping("/get")
    public BaseResponse<Team> searchAllTeam(Long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Team teamById = teamService.getById(id);
        if (teamById == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(teamById);
    }

    // todo 查询分页
    @GetMapping("/list/page")
    public BaseResponse<Page<Team>> listTeamsByPage(TeamQuery teamQuery) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamQuery, team);
        Page<Team> page = new Page<>(teamQuery.getPageNum(), teamQuery.getPageSize());
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>(team);
        Page<Team> resultPage = teamService.page(page, queryWrapper);
        return ResultUtils.success(resultPage);
    }


    @GetMapping("/list")
    public BaseResponse<List<TeamVO>> getTeamList(TeamQuery teamQuery) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        List<TeamVO> teamVOS = teamService.listTeams(teamQuery);
        return ResultUtils.success(teamVOS);
    }


    @GetMapping("/myteam")
    public BaseResponse<List<Team>> getMyTeam(HttpServletRequest httpServletRequest) {
        Object userObject = httpServletRequest.getSession().getAttribute(USER_LOGIN_KEY);
        User currentUser = (User) userObject;
        if (currentUser == null) {
            return null;
        }
        List<Team> teams = teamService.getMyTeam(currentUser);
        if (teams == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(teams);
    }

}
